<?php
class AdsiteController extends Controller {
	public function index() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS .'AdsiteService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/adsite.tpl" );
			return;
		}
		// START 数据库查询及分页数据
		// $adsiteService = new AdsiteService ( $this->getDB () );
		// $page_size = 2;
		// $page_no = $page_no < 1 ? 1 : $page_no;
		// $row_count = $adsiteService->countNum ();
		// $total_page = $row_count / $page_size == 0 ? $row_count / $page_size : ceil ( $row_count / $page_size );
		// $total_page = $total_page < 1 ? 1 : $total_page;
		// $page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
		// $start = ($page_no - 1) * $page_size;
		
		// $user_infos = $adsiteService->userPage ( $start, $page_size );
		$this->smarty->assign ( 'user_infos', $user_infos );
		$this->smarty->assign ( '_GET', $_POST );
		// $this->smarty->assign ( 'page_no', $page_no );
		// $this->smarty->assign ( 'page_html', $page_html );
		$this->smarty->display ( "admin/ad/adsite.tpl" );
	}
	public function addOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS .'AdsiteService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/adsite.tpl" );
			return;
		}
		
		$adsiteService = new AdsiteService ( $this->getDB () );
		
		$id = $_POST ["site_id"];
		if (empty ( $id )) {
			$input_data = array (
					'site_id' => md5 ( uniqid () ),
					'site_name' => $_POST ["site_name"],
					'site_description' => $_POST ["site_description"],
					'site_parent' => $_POST ["site_parent"],
					'site_type' => $_POST ["site_type"] 
			);
			$data = $adsiteService->addItem ( $input_data );
		} else {
			
			$input_data = array (
					'site_name' => $_POST ["site_name"],
					'site_description' => $_POST ["site_description"],
					'site_parent' => $_POST ["site_parent"],
					'site_type' => $_POST ["site_type"] 
			);
			
			$input_condition = array (
					'site_id' => $_POST ["site_id"] 
			);
			$data = $adsiteService->edit ( $input_data, $input_condition );
		}
		echo "1";
	}
	public function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS .'AdsiteService.class.php';
		$service = new AdsiteService ( $this->getDB () );
		$id = $_POST ['id'];
		$service->deleteItem ( $id );
		echo 1;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS .'AdsiteService.class.php';
		// START 数据库查询及分页数据
		$adsiteService = new AdsiteService ( $this->getDB () );
		
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $adsiteService->countNum ();
		$result ["total"] = $row_count;
		$user_infos = $adsiteService->userPage ( $offset, $rows );
		$items = array ();
		// while($row = $user_infos->fetch_assoc()){
		// print ($row);
		// array_push($items, $row);
		// }
		$result ["rows"] = $user_infos;
		echo json_encode ( $result );
		// $this->smarty->display("admin/user_list.tpl");
	}
	public function getStaticSites() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS .'AdsiteService.class.php';
		$service = new AdsiteService ( $this->getDB () );
		$result = $service->getStaticSites ();
		echo json_encode ( $result );;
	}
	
	
}